package com.lz.football.module.main.view
{
	import com.common.stdlib.DateUtil;
	import com.common.utils.timeUtils.TimerManager;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.MainEvent;
	import com.liangzhong.model.AppData;
	import com.lz.football.module.base.IComponent;
	
	import common.BaseView.DisplayMouseEventManager;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	/**
	 *在线时间奖励 
	 * @author Administrator
	 * 
	 */	
	public class OnlineAward extends Sprite implements IComponent
	{
		/**
		 * 
		 */
		private var _text:TextField;
		/**
		 *剩余时间 
		 */
		private var _time:int;
		private var _skin:MovieClip
		public function OnlineAward()
		{
			super();
		}
		
		public function initUI(view:Object=null):void
		{
			_skin = view as MovieClip
			_text = view.tMc.txtTime;
			_text.text = "";
			_text.mouseEnabled = false;
			addEvtListener();
		}
		
		/**
		 *设置奖励时间 
		 * 
		 */
		public function setOnlinetime():void
		{
			if(AppData.getInstance().activityDatemanager.onlineType >6)
			{
				removeEvtListener();
				if(_skin.parent)
					_skin.parent.removeChild(_skin);
				return;
			}
			addEvtListener();
			_time = AppData.getInstance().activityDatemanager.onlineTime;
			TimerManager.instance.addFunction(checkOutTime);
			DisplayMouseEventManager.instance.addTipsDisplay(_skin,"时间结束可获得:" + AppData.getInstance().activityDatemanager.onlineAwardByType());
			
			checkOutTime();
		}
		/**
		 *检测时间 
		 * 
		 */
		private function checkOutTime():void
		{
			_time--;
			AppData.getInstance().activityDatemanager.onlineTime --;
			if(_time <= 0)
			{
				_text.text = "可领取";
				TimerManager.instance.removeFunction(checkOutTime);
			}else
			{
				_text.text = DateUtil.formatShowHMS(_time,false);
				
			}
		}
		protected function onClickOnlineHandler(event:Event):void
		{
			if(AppData.getInstance().activityDatemanager.onlineTime > 0)
			{
				return;
			}
			removeEvtListener();
			AppEventDispatcher.getInstance().dispatchEvent(new MainEvent(MainEvent.ONLINEAWARD));
		}
		public function addEvtListener():void
		{
			_skin.addEventListener(MouseEvent.CLICK,onClickOnlineHandler);
		}
		
		public function removeEvtListener():void
		{
			_skin.removeEventListener(MouseEvent.CLICK,onClickOnlineHandler);
		}
		
		public function destroyView():void
		{
		}
	}
}